package com.zh.note.demo.java8;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {

        int[] nums1 = {1, 2, 3, 4, 5};
        Integer[] nums2 = {1, 2, 3, 4, 5};
        String[] strs3 = {"a", "b", "c", "d"};
        List<String> strList4 = Arrays.asList("1", "2", "3", "4");
        List<Integer> intList5 = Arrays.asList(1, 2, 3, 4, 5);
        // int数组转换成list
        List<Integer> collect1 = Arrays.stream(nums1).boxed().collect(Collectors.toList());

        // Integer数组转成list
        List<Integer> collect2 = Arrays.stream(nums2).collect(Collectors.toList());

        // String数组转list
        List<String> collect3 = Arrays.stream(strs3).collect(Collectors.toList());

        // Integer list转换成int数组
        int[] array = intList5.stream().mapToInt(new ToIntFunction<Integer>() {
            @Override
            public int applyAsInt(Integer integer) {
                return integer;
            }
        }).toArray();
        int[] array2 = intList5.stream().mapToInt(Integer::intValue).toArray();

        // String list装成Integer数组
        Integer[] array3 = strList4.stream().map(str -> {
            return Integer.parseInt(str);
        }).toArray(Integer[]::new);

        // string list转成int数组
        int[] array4 = strList4.stream().map(Integer::parseInt).mapToInt(i -> i).toArray();

        // string数组 装成字符串
        String collect = Arrays.stream(strs3).collect(Collectors.joining(","));
        String reduce = Arrays.stream(strs3).reduce("", (a, b) -> a + b);

        System.out.println(collect);

        // int 数组转成字符串
        String collect4 = Arrays.stream(nums1).boxed().map(String::valueOf).collect(Collectors.joining(","));

        // list 转换成字符串
        String collect5 = strList4.stream().collect(Collectors.joining());
        String collect6 = intList5.stream().map(String::valueOf).collect(Collectors.joining());

        // list 转换成map
        Map<String, String> collect7 = strList4.stream().collect(Collectors.toMap(String::valueOf, Function.identity(), (x, y) -> x));
    }
}
